<?php
require_once __DIR__."/vendor/autoload.php";
use Swoole\Http\Request;
use Swoole\Http\Response;

require_once __DIR__."/app/config/define.php"; //自定义配置
\Core\BeanFactory::init();//初始化Bean工厂
$dispatcher=\Core\BeanFactory::getBean("RouterCollector")->getDispatcher();

// print_r($dispatcher);exit;
/*
FastRoute\Dispatcher\GroupCountBased Object
(
    [staticRouteMap:protected] => Array
        (
            [GET] => Array
                (
                    [/test] => Closure Object
                        (
                            [static] => Array
                                (
                                    [method] => ReflectionMethod Object
                                        (
                                            [name] => test
                                            [class] => App\controllers\UserController
                                        )
                                    [instance] => App\controllers\UserController Object
                                        (
                                            [version] => 2.3
                                        )
                                )
                            [parameter] => Array
                                (
                                    [$parms] => <required>
                                )
                        )
                    [/user] => Closure Object
                        (
                            [static] => Array
                                (
                                    [method] => ReflectionMethod Object
                                        (
                                            [name] => user
                                            [class] => App\controllers\UserController
                                        )
                                    [instance] => App\controllers\UserController Object
                                        (
                                            [version] => 2.3
                                        )
                                )
                            [parameter] => Array
                                (
                                    [$parms] => <required>
                                )
                        )
                )
        )
    [variableRouteData:protected] => Array
        (
            [GET] => Array
                (
                    [0] => Array
                        (
                            [regex] => ~^(?|/getUser(\d+))$~
                            [routeMap] => Array
                                (
                                    [2] => Array
                                        (
                                            [0] => Closure Object
                                                (
                                                    [static] => Array
                                                        (
                                                            [method] => ReflectionMethod Object
                                                                (
                                                                    [name] => getUser
                                                                    [class] => App\controllers\UserController
                                                                )
                                                            [instance] => App\controllers\UserController Object
                                                                (
                                                                    [version] => 2.3
                                                                )
                                                        )
                                                    [parameter] => Array
                                                        (
                                                            [$parms] => <required>
                                                        )
                                                )
                                            [1] => Array
                                                (
                                                    [uid] => uid
                                                )
                                        )
                                )
                        )
                )
        )
)
*/

$http = new Swoole\Http\Server("0.0.0.0", 80);
$http->on('request', function (Request $request,Response $response) use($dispatcher) {
    $myrequest=\Core\http\Request::init($request);
    $routeInfo = $dispatcher->dispatch($myrequest->getMethod(),$myrequest->getUri() );
   //[1,$handler,$var]
//    print_r($routeInfo);return;
/*
Array
(
    [0] => 1
    [1] => Closure Object
        (
            [static] => Array
                (
                    [method] => ReflectionMethod Object
                        (
                            [name] => getUser
                            [class] => App\controllers\UserController
                        )
                    [instance] => App\controllers\UserController Object
                        (
                            [version] => 2.3
                        )
                )
            [parameter] => Array
                (
                    [$parms] => <required>
                )
        )
    [2] => Array
        (
            [uid] => 123 //@RequestMapping(value="/getUser/{uid:\d+}")
        )
)
*/
    switch ($routeInfo[0]) {
        case FastRoute\Dispatcher::NOT_FOUND:
            $response->status(404);
            $response->end();
            break;
        case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
            $response->status(405);
            $response->end();
            break;
        case FastRoute\Dispatcher::FOUND:
            $handler = $routeInfo[1];
            $vars=$routeInfo[2];
            $response->end($handler($vars));//最终执行的目标方法
            break;
    }

});
$http->start();

